package de.dhbw.tinf11b.vfb;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import de.dhbw.tinf11b.vfb.exceptions.MarkNotFoundException;
import de.dhbw.tinf11b.vfb.exceptions.StudentNotFoundException;

public class StudentManager {
	private static StudentManager instance = null;
	
	public static StudentManager getInstance() {
		if(instance == null) {
			instance = new StudentManager();
		}
		
		return instance;
	}
	
	private StudentManager() {
		
	}

	protected List<Student> students = new LinkedList<Student>();
	

	/**
	 * Add a student.
	 * @param firstName
	 * @param lastName
	 * @param homeAddress
	 * @param studentAddress
	 * @param email
	 * @param dateOfBirth
	 * @param yearOfAbitur
	 * @param placeOfAbitur
	 * @param abiturMark
	 * @param company
	 * @param trainingManager
	 * @param matriculationNumber
	 */
	public void addStudent(String firstName, String lastName,
			String homeAddress, String studentAddress, String email,
			Date dateOfBirth, int yearOfAbitur, String placeOfAbitur,
			float abiturMark, String company,
			String trainingManager,
			String matriculationNumber) {
		
		Student student = new Student();
		student.firstName = firstName;
		student.lastName = lastName;
		student.homeAddress = homeAddress;
		student.studentAddress = studentAddress;
		student.email = email;
		student.dateOfBirth = dateOfBirth;
		student.yearOfAbitur = yearOfAbitur;
		student.placeOfAbitur = placeOfAbitur;
		student.abiturMark = abiturMark;
		student.company = company;
		student.trainingManager = trainingManager;
		student.matriculationNumber = matriculationNumber;
		
		students.add(student);
		
	}
	
	/**
	 * Retrieve a student by name and date of birth.
	 * @param firstName
	 * @param lastName
	 * @param dateOfBirth
	 * @return
	 * @throws StudentNotFoundException 
	 */
	public Student getStudentByNameAndBirthDate(String firstName, String lastName, Date dateOfBirth) throws StudentNotFoundException {
		for (Student student : students) {
			if (student.firstName.equals(firstName) && student.lastName.equals(lastName) && student.dateOfBirth.equals(dateOfBirth)) {
				return student;
			}
		}
		throw new StudentNotFoundException();
	}
	
	/**
	 * Retrieve a student by matriculation number.
	 * @param matriculationNumber
	 * @return
	 * @throws StudentNotFoundException 
	 */
	public Student getStudentByMatriculationNumber(String matriculationNumber) throws StudentNotFoundException {
		for (Student student : students) {
			if (student.matriculationNumber.equals(matriculationNumber)) {
				return student;
			}
		}
		throw new StudentNotFoundException();
	}

	
	/**
	 * Print all Marks of a Unit.
	 * @param unit
	 * @return
	 * @throws MarkNotFoundException
	 */
	public String printMarkList(Unit unit) throws MarkNotFoundException{
		String markList="";
		for (Student student : students){
			if (student.getMark(unit) instanceof ResultMark){
				ResultMark resultMark = (ResultMark)student.getMark(unit);
				markList = markList + student.getLastName() + ", " + student.getFirstName() + resultMark.getResult() + "\n";
			}
			if (student.getMark(unit) instanceof PassedMark){
				PassedMark passedMark = (PassedMark)student.getMark(unit);
				markList = markList + student.getLastName() + ", " + student.getFirstName() + passedMark.getResult() + "\n";
			}
		}
		return markList;
	}

}